package com.juzipi.demo.handler;

import com.alibaba.fastjson.JSON;
import com.juzipi.demo.tools.Result;
import com.juzipi.demo.tools.ResultTool;
import com.juzipi.demo.utils.JsonAuthentication;
import com.juzipi.demo.utils.ResultCode;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功自定义处理
 */
@Component
public class MyAuthenticationSuccessHandler extends JsonAuthentication implements AuthenticationSuccessHandler {


    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //只需要以json格式返回一个提示
        Result result = ResultTool.successCustomize(ResultCode.LOGIN_SUCCESSFULLY,null);
//        String jsonString = JSON.toJSONString(result);
//        httpServletResponse.setContentType("text/json;charset=utf-8");
//        httpServletResponse.getWriter().write(jsonString);
        this.WriteJson(httpServletRequest,httpServletResponse,result);
    }
}
